package com.ssy.lingxi.search.controller.pc;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.*;
import com.ssy.lingxi.search.entity.Purchase;
import com.ssy.lingxi.search.model.response.PurchaseResponse;
import com.ssy.lingxi.search.service.IPurchaseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @menu: 能力中心--代客下单的进货单管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/9/4
 */
@RestController
@RequestMapping("/search/customer/purchase")
public class PurchaseCustomerController extends BaseController {

    @Resource
    private IPurchaseService purchaseService;

    /**
     * 查询进货单列表
     * @param customerMemberId 客户会员id
     * @param customerMemberRoleId 客户会员角色id
     * @param orderId 订单id
     */
    @RequestMapping(value = "/getPurchaseList", method = RequestMethod.GET)
    public Wrapper<List<PurchaseResponse>> getPurchaseList(@RequestParam("customerMemberId") Long customerMemberId, @RequestParam("customerMemberRoleId") Long customerMemberRoleId, @RequestParam(value = "orderId", required = false) Long orderId) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(purchaseService.getPurchaseList(sysUser, shopId, true, customerMemberId, customerMemberRoleId, orderId));
    }

    /**
     * 新增/修改进货单
     * @param purchaseRequest 进货单实体
     * @return 进货单数量
     */
    @RequestMapping(value = "/saveOrUpdatePurchase", method = RequestMethod.POST)
    public Wrapper<Integer> saveOrUpdatePurchase(@RequestBody @Valid PurchaseCustomerRequest purchaseRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        Purchase purchase = BeanUtil.copyProperties(purchaseRequest, Purchase.class);
        purchase.setShopId(shopId);
        return Wrapper.success(purchaseService.saveOrUpdatePurchase(sysUser, purchase));
    }

    /**
     * 批量新增进货单
     * @param purchaseBatchRequest 进货单实体
     * @return 是否成功
     */
    @RequestMapping(value = "/savePurchaseBatch", method = RequestMethod.POST)
    public Wrapper<Boolean> savePurchaseBatch(@RequestBody PurchaseCustomerBatchRequest purchaseBatchRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        List<PurchaseCustomerRequest> purchaseCustomerList = purchaseBatchRequest.getPurchaseBatchList();
        return Wrapper.success(purchaseService.savePurchaseCustomerBatch(sysUser, shopId, purchaseCustomerList));
    }

    /**
     * 删除进货单
     * @param simpleIdListRequest 参数
     */
    @RequestMapping(value = "/deletePurchase", method = RequestMethod.POST)
    public Wrapper<String> deletePurchase(@RequestBody SimpleIdListRequest simpleIdListRequest){
        return Wrapper.success(purchaseService.deletePurchase(simpleIdListRequest));
    }

    /**
     * 查询进货单数量
     * @param customerMemberId 客户会员id
     * @param customerMemberRoleId 客户会员角色id
     * @param orderId 订单id
     */
    @RequestMapping(value = "/getPurchaseCount", method = RequestMethod.GET)
    public Wrapper<Integer> getPurchaseCount(@RequestParam("customerMemberId") Long customerMemberId, @RequestParam("customerMemberRoleId") Long customerMemberRoleId, @RequestParam(value = "orderId", required = false) Long orderId){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(purchaseService.getPurchaseCount(sysUser, shopId, customerMemberId, customerMemberRoleId, orderId));
    }
}
